import {Graphics} from "../Thing/Graphics.js"
import {CustomDataSource} from "../dataSource/CustomDataSource.js"
import {GraphicsHelper} from '../Helper/GraphicsHelper.js';
class GraphicsLayer {
	/**
	 * 点、线、面管理
	 * @description 
	 * @param {Object} viewer
	 */
	constructor(viewer) {
		this._viewer = viewer;
		this._scene = viewer.scene;
		this._group = new Map();
	}
	
	hasGroup(id){
		return this._group.has(id);
	}
	
	getGroup(id){
		if(this.hasGroup(id)){
			return this._group.get(id);
		}
	}
	
	setGroup(id,group){
		this._group.set(id,group);
	}
	
	/**
	 * @description 添加
	 * @param {Object} options{
		 group:{
			 id:分组标识,
			 name:分组名称,
		 },
		 graphics:{
		 },
	 }
	 */
	add(options,complete){
		let group;
		if(Cesium.defined(options.group)&& Cesium.defined(options.group.id)){
			if(this.hasGroup(options.group.id)){
				group=this.getGroup(options.group.id);
			}else{
				group=new CustomDataSource(options.group);
				this._viewer.dataSources.add(group);
				this.setGroup(group.id,group);
			}
		}
		let graphics=GraphicsHelper.create(options);
		if(group!=null){
			let point=group.add(graphics);
			if (complete) {
				complete({
					code: 200,
					message: '成功.',
					data:point
				});
			}
		}else{
			let point= this._viewer.entities.add(graphics);
			if (complete) {
				complete({
					code: 200,
					message: '成功.',
					data:point
				});
			}
			return point;
		}
	}
	
	/**
	 * @description 获取对象
	 * @param {Object} options
	 */
	get(options) {
		let groupId=options.groupId;
		let id=options.id;
		let graphics;
		if(Cesium.defined(groupId)){
			let group= this.getGroup(groupId);
			if(Cesium.defined(group)){
				graphics=group.getById(id);
			}
		}
		
		if(!Cesium.defined(graphics)){
			graphics=this._viewer.entities.getById(id);
		}
		
		if(Cesium.defined(graphics)){
			graphics.show=true
			if(Cesium.defined(options.complete)){
				options.complete({
					code:200,
					message:'成功！',
					data:graphics
				});
			}
		}else{
			if(Cesium.defined(options.complete)){
				options.complete({
					code:500,
					message:'未查询到对象！',
				});
			}
		}
	}
	
	/**
	 * @description 移出
	 * @param {Object} options{
		groupId:分组Id,
		id:对象Id
	 }
	 */
	remove(options){
		let groupId=options.groupId;
		let id=options.id;
		let graphics;
		if(Cesium.defined(groupId)){
			let group= this.getGroup(groupId);
			if(Cesium.defined(group)){
				return group.removeById(id);
			}
		}
		return this._viewer.entities.removeById(id);
	}
	
	/**
	 * @description 显示
	 * @param {Object} options{
		groupId:分组Id,
		id:对象Id
	 }
	 */
	show(options){
		let groupId=options.groupId;
		let id=options.id;
		let graphics;
		if(Cesium.defined(groupId)){
			let group= this.getGroup(groupId);
			if(Cesium.defined(group)){
				graphics=group.getById(id);
			}
		}
		if(!Cesium.defined(graphics)){
			graphics=this._viewer.entities.getById(id);
		}
		if(Cesium.defined(graphics)){
			graphics.show=true
			if(Cesium.defined(options.complete)){
				options.complete({
					code:200,
					message:'成功！',
					data:graphics
				});
			}
		}else{
			if(Cesium.defined(options.complete)){
				options.complete({
					code:500,
					message:'未查询到对象！',
				});
			}
		}
	}
	
	/**
	 * @description 隐藏
	 * @param {Object} options{
		groupId:分组Id,
		id:对象Id
	 }
	 */
	hide(options){
		let groupId=options.groupId;
		let id=options.id;
		let graphics;
		if(Cesium.defined(groupId)){
			let group= this.getGroup(groupId);
			if(Cesium.defined(group)){
				graphics=group.getById(id);
			}
		}
		
		if(!Cesium.defined(graphics)){
			graphics=this._viewer.entities.getById(id);
		}
		
		if(Cesium.defined(graphics)){
			graphics.show=false
			if(Cesium.defined(options.complete)){
				options.complete({
					code:200,
					message:'成功！',
					data:graphics
				});
			}
		}else{
			if(Cesium.defined(options.complete)){
				options.complete({
					code:500,
					message:'未查询到对象！',
				});
			}
		}
	}
	
	/**
	 * @description 更新
	 * @param {Object} options
	 */
	update(options){
		let groupId=options.groupId;
		let id=options.id;
		let graphics;
		if(Cesium.defined(groupId)){
			let group= this.getGroup(groupId);
			if(Cesium.defined(group)){
				graphics=group.getById(id);
			}
		}
		if(!Cesium.defined(graphics)){
			graphics=this._viewer.entities.getById(id);
		}
		if(Cesium.defined(graphics)){
			graphics.update(options.graphics);
			if(Cesium.defined(options.complete)){
				options.complete({
					code:200,
					message:'成功！',
					data:graphics
				});
			}
		}else{
			if(Cesium.defined(options.complete)){
				options.complete({
					code:500,
					message:'未查询到对象！',
				});
			}
		}
	}
	
	/**
	 * @description 销毁
	 */
	destroy() {
		delete this._group
		delete this._scene
		delete this._viewer
		return Cesium.destroyObject(this);
	}
}
export {GraphicsLayer}
